var gulp = require('gulp'),
	// data = require('gulp-data'),
	uglify = require('gulp-uglify'),
	minCss = require('gulp-minify-css'),
	javascriptObfuscator = require('gulp-javascript-obfuscator'),
	minHtml = require('gulp-htmlmin'),
	clean = require('gulp-clean');
	// path = require('path'),
	// express = require('express'),
	// fs = require('fs');

// css 压缩 
gulp.task('css', function() {
	var distPath = './dist/static';
	var cssFiles = './static/*.css';

	return gulp.src(cssFiles)
		.pipe(minCss())
		// .pipe(rename({suffix:'.min'}))
		.pipe(gulp.dest(distPath));
});

// function css(){
// 	var distPath = './dist/static';
// 	var cssFiles = './static/*.css';
// 	return gulp.src(cssFiles)
// 		.pipe(minCss())
// 		// .pipe(rename({suffix:'.min'}))
// 		.pipe(gulp.dest(distPath));
// }

// exports.css = css

// js 压缩  
gulp.task('js', function() {
	var distPath = './dist/static';
	var jsFiles = './static/*.js';

	return gulp.src(jsFiles)
		.pipe(uglify())
		.pipe(javascriptObfuscator())
		// .pipe(rename({suffix:'.min'}))
		.pipe(gulp.dest(distPath));
});

// img
gulp.task('img', function() {
	var distPath = './dist/static';
	var imgFiles = './static/*.png';

	return gulp.src(imgFiles, { encoding: false })
		.pipe(gulp.dest(distPath));
});

gulp.task('html',function() {
	var pages = ['./*.html'];
	const options = {
		removeComments: true, //清除HTML注释
    collapseWhitespace: false, //压缩HTML
    collapseBooleanAttributes: false, //省略布尔属性的值 <input checked=”true”/> ==> <input />
    removeEmptyAttributes: false, //删除所有空格作属性值 <input id=”” /> ==> <input />
    removeScriptTypeAttributes: false, //删除<script>的type=”text/javascript”
    removeStyleLinkTypeAttributes: false, //删除<style>和<link>的type=”text/css”
    minifyJS: true, //压缩页面JS
    minifyCSS: true //压缩页面CSS
	}
	return gulp.src(pages)
			// .pipe(data(() => ({name:"app test"})))
			// .pipe(data(() => require('./src/static/js/data_pro1.js')))
			// .pipe(nunjucks.compile())
			// .pipe(rename({extname:'.html'}))
			.pipe(minHtml(options))
			.pipe(gulp.dest('dist'));
});

gulp.task('clean',function(){
	return gulp.src('./dist', { allowEmpty: true })
			   .pipe(clean());
});
// function clean(){
// 	return gulp.src('./dist')
// 			   .pipe(clean());
// }

// gulp.task('build', gulp.series('clean', function(){
// 	gulp.start('html','css','js', 'img');
// }))

gulp.task('build', gulp.series('clean', gulp.parallel('html','css','js', 'img')))

